home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTAccess.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  11.1 KB  |  477 lines

  1. /*        Access Manager                    HTAccess.c
  2. **        ==============
  3. **
  4. ** Authors
  5. **    TBL    Tim Berners-Lee timbl@info.cern.ch
  6. **    JFG    Jean-Francois Groff jfg@dxcern.cern.ch
  7. **    DD    Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
  8. ** History
  9. **     8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
  10. **    26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
  11. **     6 Oct 92 Moved HTClientHost and logfile into here. TBL
  12. **    17 Dec 92 Tn3270 added, bug fix. DD
  13. **     4 Feb 93 Access registration, Search escapes bad chars TBL
  14. **          PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
  15. **    28 May 93 WAIS gateway explicit if no WAIS library linked in.
  16. **
  17. ** Bugs
  18. **    This module assumes that that the graphic object is hypertext, as it
  19. **    needs to select it when it has been loaded.  A superclass needs to be
  20. **    defined which accepts select and select_anchor.
  21. */
  22.  
  23. #ifndef DEFAULT_WAIS_GATEWAY
  24. #define DEFAULT_WAIS_GATEWAY "http://www.ncsa.uiuc.edu:8001/"
  25. #endif
  26.  
  27. /* Implements:
  28. */
  29. #include "HTAccess.h"
  30.  
  31. /* Uses:
  32. */
  33.  
  34. #include "HTParse.h"
  35. #include "HTUtils.h"
  36. #include "HTML.h"               /* SCW */
  37.  
  38. #include <stdio.h>
  39.  
  40. #include "HTList.h"
  41. #include "HText.h"      /* See bugs above */
  42. #include "HTAlert.h"
  43.  
  44. /* #define TRACE 1 */
  45.  
  46.  
  47. #ifndef _AMIGA
  48.  /* in the mean time, looks ar X resources, may want to make
  49.     it look in prefs file later */
  50. extern char *mo_check_for_proxy (char *);
  51. #endif
  52.  
  53.  
  54.  
  55. /*    These flags may be set to modify the operation of this module
  56. */
  57. PUBLIC char * HTClientHost = 0; /* Name of remote login host if any */
  58.  
  59. /*    To generate other things, play with these:
  60. */
  61.  
  62. PUBLIC HTFormat HTOutputFormat = NULL;
  63. PUBLIC HTStream* HTOutputStream = NULL; /* For non-interactive, set this */
  64.  
  65. PUBLIC BOOL using_gateway = NO; /* are we using a gateway? */
  66. PUBLIC BOOL using_proxy = NO; /* are we using a proxy gateway? */
  67.  
  68.  
  69. PRIVATE HTList * protocols = NULL;   /* List of registered protocol descriptors */
  70.  
  71.  
  72. /*    Register a Protocol                HTRegisterProtocol
  73. **    -------------------
  74. */
  75.  
  76. PUBLIC BOOL HTRegisterProtocol(protocol)
  77.     HTProtocol * protocol;
  78. {
  79.     if (!protocols) protocols = HTList_new();
  80.     HTList_addObject(protocols, protocol);
  81.     return YES;
  82. }
  83.  
  84.  
  85. /*    Register all known protocols
  86. **    ----------------------------
  87. **
  88. **    Add to or subtract from this list if you add or remove protocol modules.
  89. **    This routine is called the first time the protocol list is needed,
  90. **    unless any protocols are already registered, in which case it is not called.
  91. **    Therefore the application can override this list.
  92. */
  93. PRIVATE void HTAccessInit NOARGS            /* Call me once */
  94. {
  95. extern HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
  96. extern HTProtocol HTFTP, HTNews, HTGopher, HTMail;
  97. #ifdef DIRECT_WAIS
  98. extern HTProtocol HTWAIS;
  99. #endif
  100.     HTRegisterProtocol(&HTFTP);
  101.     HTRegisterProtocol(&HTNews);
  102.     HTRegisterProtocol(&HTGopher);
  103.     HTRegisterProtocol(&HTMail);
  104. #ifdef DIRECT_WAIS
  105.     HTRegisterProtocol(&HTWAIS);
  106. #endif
  107.  
  108.     HTRegisterProtocol(&HTTP);
  109.     HTRegisterProtocol(&HTFile);
  110.     HTRegisterProtocol(&HTTelnet);
  111.     HTRegisterProtocol(&HTTn3270);
  112.     HTRegisterProtocol(&HTRlogin);
  113. }
  114.  
  115.  
  116. /*        Find physical name and access protocol
  117. **        --------------------------------------
  118. **
  119. **
  120. ** On entry,
  121. **    addr        must point to the fully qualified hypertext reference.
  122. **    anchor        a pareent anchor with whose address is addr
  123. **
  124. ** On exit,
  125. **    returns     HT_NO_ACCESS        Error has occured.
  126. **            HT_OK            Success
  127. **
  128. */
  129. PRIVATE int get_physical ARGS2(
  130.     char *,     addr,
  131.     HTParentAnchor *,    anchor)
  132. {
  133.     char * access=0;    /* Name of access method */
  134.     char * physical = 0;
  135.  
  136.     HTAnchor_setPhysical(anchor, addr);
  137.  
  138.     access =  HTParse(HTAnchor_physical(anchor),
  139.         "file:", PARSE_ACCESS);
  140.  
  141. /*    Check whether gateway access has been set up for this
  142. */
  143. #define USE_GATEWAYS
  144. #ifdef USE_GATEWAYS
  145.     {
  146.     char *gateway_parameter, *gateway, *proxy;
  147.  
  148.     /* search for gateways */
  149.     gateway_parameter = (char *)malloc(strlen(access)+20);
  150.     if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
  151.     strcpy(gateway_parameter, "WWW_");
  152.     strcat(gateway_parameter, access);
  153.     strcat(gateway_parameter, "_GATEWAY");
  154.     gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
  155.  
  156.     /* search for proxy servers */
  157.     strcpy(gateway_parameter, access);
  158.     strcat(gateway_parameter, "_proxy");
  159.     proxy = (char *)getenv(gateway_parameter);
  160.     free(gateway_parameter);
  161.  
  162.     /*
  163.      * The environment variables have precedence over the X resources,
  164.      * But if we got no proxy from them, check the X resources now.
  165.      */
  166. #ifndef _AMIGA
  167. /* see above */
  168.     if ((proxy == NULL)||(proxy[0] == '\0'))
  169.     {
  170.         proxy = mo_check_for_proxy(access);
  171.     }
  172. #endif
  173.  
  174. #ifndef DIRECT_WAIS
  175.     if (!gateway && 0==strcmp(access, "wais")) {
  176.         gateway = DEFAULT_WAIS_GATEWAY;
  177.     }
  178. #endif
  179.  
  180.     /* make sure the using_proxy variable is false */
  181.     using_proxy = NO;
  182.  
  183.     /* proxy servers have precedence over gateway servers */
  184.     if (proxy) {
  185.         char * gatewayed;
  186.  
  187.         gatewayed = NULL;
  188.         StrAllocCopy(gatewayed,proxy);
  189.         StrAllocCat(gatewayed,addr);
  190.         using_proxy = YES;
  191.         HTAnchor_setPhysical(anchor, gatewayed);
  192.         free(gatewayed);
  193.         free(access);
  194.  
  195.         access =  HTParse(HTAnchor_physical(anchor),
  196.             "http:", PARSE_ACCESS);
  197.     } else if (gateway) {
  198.         char * gatewayed;
  199.  
  200.         gatewayed = NULL;
  201.         StrAllocCopy(gatewayed,gateway);
  202.         StrAllocCat(gatewayed,addr);
  203.         using_gateway = YES;
  204.         HTAnchor_setPhysical(anchor, gatewayed);
  205.         free(gatewayed);
  206.         free(access);
  207.  
  208.         access =  HTParse(HTAnchor_physical(anchor),
  209.             "http:", PARSE_ACCESS);
  210.     } else {
  211.         using_proxy = NO;
  212.         using_gateway = NO;
  213.     }
  214.     }
  215. #endif
  216.  
  217.  
  218.  
  219. /*    Search registered protocols to find suitable one
  220. */
  221.     {
  222.     int i, n;
  223.     if (!protocols) HTAccessInit();
  224.     n = HTList_count(protocols);
  225.     for (i=0; i<n; i++) {
  226.         HTProtocol *p = HTList_objectAt(protocols, i);
  227.         if (strcmp(p->name, access)==0) {
  228.         HTAnchor_setProtocol(anchor, p);
  229.         free(access);
  230.         return (HT_OK);
  231.         }
  232.     }
  233.     }
  234.  
  235.     free(access);
  236.     return HT_NO_ACCESS;
  237. }
  238.  
  239.  
  240. /*        Load a document
  241. **        ---------------
  242. **
  243. **    This is an internal routine, which has an address AND a matching
  244. **    anchor.  (The public routines are called with one OR the other.)
  245. **
  246. ** On entry,
  247. **    addr        must point to the fully qualified hypertext reference.
  248. **    anchor        a pareent anchor with whose address is addr
  249. **
  250. ** On exit,
  251. **    returns     <0        Error has occured.
  252. **            HT_LOADED    Success
  253. **            HT_NO_DATA    Success, but no document loaded.
  254. **                    (telnet sesssion started etc)
  255. **
  256. */
  257. PRIVATE int HTLoad ARGS4(
  258.     CONST char *,        addr,
  259.     HTParentAnchor *,    anchor,
  260.     HTFormat,        format_out,
  261.     HTStream *,        sink)
  262. {
  263.     HTProtocol* p;
  264.     int status = get_physical(addr, anchor);
  265.     if (status < 0) return status;    /* Can't resolve or forbidden */
  266.  
  267.     p = HTAnchor_protocol(anchor);
  268.     return (*(p->load))(HTAnchor_physical(anchor),
  269.             anchor, format_out, sink);
  270. }
  271.  
  272.  
  273. /*        Get a save stream for a document
  274. **        --------------------------------
  275. */
  276. PUBLIC HTStream *HTSaveStream ARGS1(HTParentAnchor *, anchor)
  277. {
  278.     HTProtocol * p = HTAnchor_protocol(anchor);
  279.     if (!p) return NULL;
  280.  
  281.     return (*p->saveStream)(anchor);
  282.  
  283. }
  284.  
  285.  
  286. /*        Load a document - with logging etc
  287. **        ----------------------------------
  288. **
  289. **    - Checks or documents already loaded
  290. **    - Logs the access
  291. **    - Allows stdin filter option
  292. **    - Trace ouput and error messages
  293. **
  294. **    On Entry,
  295. **      anchor        is the node_anchor for the document
  296. **      full_address        The address of the document to be accessed.
  297. **      filter        if YES, treat stdin as HTML
  298. **
  299. **    On Exit,
  300. **      returns    1       Success in opening document
  301. **             0       Failure
  302. **             -1    Interrupted
  303. **
  304. */
  305.  
  306. /* This is exported all the way to gui-documents.c at the moment,
  307.    to tell mo_load_window_text when to use a redirected URL instead. */
  308. char *use_this_url_instead;
  309.  
  310. PRIVATE int HTLoadDocument ARGS4(
  311.     CONST char *,        full_address,
  312.     HTParentAnchor *,    anchor,
  313.     HTFormat,        format_out,
  314.     HTStream*,        sink)
  315. {
  316.     int     status;
  317.     HText *    text;
  318.  
  319.     use_this_url_instead = NULL;
  320.  
  321.     /* We LOVE goto's! */
  322.   try_again:
  323.     if (TRACE) fprintf (stderr,
  324.       "HTAccess: loading document %s\n", full_address);
  325.  
  326.     status = HTLoad(full_address, anchor, format_out, sink);
  327.  
  328.     if (status == HT_LOADED) {
  329.     if (TRACE) {
  330.         fprintf(stderr, "HTAccess: `%s' has been accessed.\n",
  331.         full_address);
  332.     }
  333.     return 1;
  334.     }
  335.  
  336.     if (status == HT_REDIRECTING)
  337.       {
  338.     /* Exported from HTMIME.c, of all places. */
  339.     extern char *redirecting_url;
  340.     if (TRACE)
  341.       {
  342.         fprintf (stderr, "HTAccess: '%s' is a redirection URL.\n",
  343.              full_address);
  344.         fprintf (stderr, "HTAccess: Redirecting to '%s'\n",
  345.              redirecting_url);
  346.       }
  347.     full_address = redirecting_url;
  348.     use_this_url_instead = full_address;
  349.     goto try_again;
  350.       }
  351.  
  352.     if (status == HT_INTERRUPTED)
  353.       {
  354.     if (TRACE)
  355.       fprintf (stderr,
  356.            "HTAccess: We were interrupted.\n");
  357.     return -1;
  358.       }
  359.  
  360.     if (status == HT_NO_DATA) {
  361.     if (TRACE) {
  362.         fprintf(stderr,
  363.         "HTAccess: `%s' has been accessed, No data left.\n",
  364.         full_address);
  365.     }
  366.     return 0;
  367.     }
  368.  
  369.     if (status<0) {              /* Failure in accessing a document */
  370.     if (TRACE) fprintf(stderr,
  371.                "HTAccess: Can't access `%s'\n", full_address);
  372.     return 0;
  373.     }
  374.  
  375.     /* If you get this, then please find which routine is returning
  376.        a positive unrecognised error code! */
  377.  
  378.     if (TRACE)
  379.       fprintf(stderr,
  380.           "**** HTAccess: socket or file number %d returned by obsolete load routine!\n", status);
  381.     return 0;
  382.  
  383. } /* HTLoadDocument */
  384.  
  385.  
  386.  
  387. /*        Load a document from absolute name
  388. **        ---------------
  389. **
  390. **    On Entry,
  391. **      addr       The absolute address of the document to be accessed.
  392. **      filter   if YES, treat document as HTML
  393. **
  394. **    On Exit,
  395. **      returns    1       Success in opening document
  396. **             0        Failure
  397. **             -1      Interrupted
  398. **
  399. **
  400. */
  401.  
  402. PUBLIC int HTLoadAbsolute ARGS1(CONST char *,addr)
  403. {
  404.    return HTLoadDocument( addr,
  405.         HTAnchor_parent(HTAnchor_findAddress(addr)),
  406.             HTOutputFormat ? HTOutputFormat : WWW_PRESENT,
  407.             HTOutputStream);
  408. }
  409.  
  410.  
  411. /*        Load a document from absolute name to stream
  412. **        --------------------------------------------
  413. **
  414. **    On Entry,
  415. **      addr       The absolute address of the document to be accessed.
  416. **      sink       if non-NULL, send data down this stream
  417. **
  418. **    On Exit,
  419. **      returns    YES     Success in opening document
  420. **             NO      Failure
  421. **
  422. **
  423. */
  424.  
  425. PUBLIC BOOL HTLoadToStream ARGS3(
  426.         CONST char *,    addr,
  427.         BOOL,        filter,
  428.         HTStream *,    sink)
  429. {
  430.    return HTLoadDocument(addr,
  431.         HTAnchor_parent(HTAnchor_findAddress(addr)),
  432.             HTOutputFormat ? HTOutputFormat : WWW_PRESENT,
  433.             sink);
  434. }
  435.  
  436.  
  437.  
  438.  
  439. /*        Load a document from relative name
  440. **        ---------------
  441. **
  442. **    On Entry,
  443. **      relative_name     The relative address of the document
  444. **                to be accessed.
  445. **
  446. **    On Exit,
  447. **      returns    YES     Success in opening document
  448. **             NO      Failure
  449. **
  450. **
  451. */
  452.  
  453. PUBLIC BOOL HTLoadRelative ARGS2(
  454.         CONST char *,        relative_name,
  455.         HTParentAnchor *,    here)
  456. {
  457.     char *        full_address = 0;
  458.     BOOL        result;
  459.     char *        mycopy = 0;
  460.     char *        stripped = 0;
  461.     char *        current_address =
  462.                 HTAnchor_address((HTAnchor*)here);
  463.  
  464.     StrAllocCopy(mycopy, relative_name);
  465.  
  466.     stripped = HTStrip(mycopy);
  467.     full_address = HTParse(stripped,
  468.            current_address,
  469.            PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
  470.     result = HTLoadAbsolute(full_address);
  471.     free(full_address);
  472.     free(current_address);
  473.     free(mycopy);
  474.     return result;
  475. }
  476.  
  477.